<?php

namespace app\common\validate;

class TaskValidate extends BaseValidate
{
    /**
     * 定义验证规则
     * 格式：'字段名'	=>	['规则1','规则2'...]
     *
     * @var array
     */	
	protected $rule = [
        'id'=>'require|integer|>:0',
        'title'=>'require',
        'money'=>'require|float|>:0',
        'imglist'=>'require',
        'task_class_id'=>'require|integer|>:0|isTaskClassExist',
        'page'=>'require|integer|>:0',
        'service_type'=>'require|number',
        'task_id'=>'require|integer|>:0|isTaskExist',
    ];
    
    /**
     * 定义错误信息
     * 格式：'字段名.规则名'	=>	'错误信息'
     *
     * @var array
     */
    protected $message = [

    ];

    // 定义验证场景
    protected $scene = [
        'create'=>['title','imglist','token','task_class_id','service_type','money'],
        'detail'=>['id'],
        'list'=>['page'],
        'receiver'=>['task_id'],
        'complete'=>['sceneComplete'],
        'isCurrentUser'=>['task_id'],
        'cancel'=>['sceneCancel']
    ];

    // 任务是否可以取消
    public function sceneCancel()
    {
        return $this->only(['task_id'])->append('task_id','isTaskCancel');
    }

    // 任务是否可以完成
    public function sceneComplete()
    {
        return $this->only(['task_id'])->append('task_id','isTaskComplete');
    }

    // 验证任务是否可以进行完成操作 只有进行中状态可以进行
    protected function isTaskComplete($value, $rule='', $data='', $field='')
    {
        $status = \app\common\model\Task::field('status')->find($value)['status'];
        if ($status == 2) {
            return true;
        }
        return "该任务不可进行完成操作";
    }
}
